package com.earl.gsm.web.system;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.earl.gsm.commons.vo.DwzMessage;
import com.earl.gsm.domain.sercurity.role.Role;
import com.earl.gsm.domain.sercurity.user.User;
import com.earl.gsm.service.PrivilegeService;
import com.earl.gsm.service.RoleService;
import com.earl.gsm.web.BaseController;
import com.wordnik.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/system/role")
//@Api(value="Person Rest Service") //修改生成的son路径
public class RoleController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleService service;
    
    @Autowired
    private PrivilegeService privilegeService;


    /**
     * GET /users -> get all the users
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ApiOperation(value = "得到所有比赛简易信息", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ModelAndView getAll() {
        log.debug("REST request to get all Users");
        return new ModelAndView("role/allRole","roleList",service.findAll());
    }
    
    @RequestMapping(value = "/preAdd")
    public ModelAndView preAdd(){
    	
    	return new ModelAndView("role/addRole","privilegeList",privilegeService.findAll());
    }
    
    @RequestMapping(value = "/add")
    public ResponseEntity<DwzMessage> add(Role role,Integer[] privilegeId){
    	service.addRoleInfo(role,privilegeId);
    	DwzMessage dodo =new DwzMessage();
    	return new ResponseEntity<DwzMessage>(dodo,HttpStatus.OK);
    }
    
    @RequestMapping(value = "/preUpdate/{roleId}")
    public ModelAndView preUpdate(@PathVariable Long roleId){

    	return new ModelAndView("role/preUpdate","role",service.findOne(roleId)).addObject("privilegeList", privilegeService.findAll()).addObject("privilegeList2", service.findRolePrivilege(roleId));
    }
    
    @RequestMapping(value = "/update")
    public ResponseEntity<DwzMessage> update(Role role,Integer[] privilegeId){
    	
    	service.updateRoleInfo(role,privilegeId);
    	
//    	service.updateByPrimaryKeySelective(role);
    	
    	DwzMessage dodo =new DwzMessage();
    	return new ResponseEntity<DwzMessage>(dodo,HttpStatus.OK);
    }
    
    @RequestMapping(value = "/delete/{roleId}")
    public ResponseEntity<DwzMessage> delete(@PathVariable Long roleId){
    	service.delete(roleId);
    	DwzMessage dwz = new DwzMessage();
    	return new ResponseEntity<DwzMessage>(dwz,HttpStatus.OK);
    }
}
